package com.yc.eduservice.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hz.apiservice.client.StudentApi;
import com.hz.apiservice.client.TeacherApi;
import com.hz.apiservice.model.dto.StudentDTO;
import com.hz.apiservice.model.dto.TeacherDTO;
import com.hz.apiservice.model.enums.ApiResponseCodeEnum;
import com.hz.apiservice.model.request.ApiResponse;
import com.yc.eduservice.model.dto.UserSelectCourseDTO;
import com.yc.eduservice.model.entities.CourseEntity;
import com.yc.eduservice.model.entities.UserCourseEntity;
import com.yc.eduservice.service.StudentService;
import com.yc.eduservice.mapper.CourseMapper;
import com.yc.eduservice.mapper.UserCourseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    CourseMapper courseMapper;
    @Resource
    UserCourseMapper userCourseMapper;
    @Resource
    TeacherApi teacherApi;
    @Resource
    StudentApi studentApi;

    @Override
    public void editCourse(int id, String courseName, String courseCode, int teacherId, int sectionCount, String courseTime, int maxStudentCount, String courseProfile, String textbook, int creditsCount, int allScore, int passScore) {
        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setCourse_name(courseName);
        courseEntity.setCourse_code(courseCode);
        courseEntity.setSection_count(sectionCount);
        courseEntity.setCourse_time(courseTime);
        courseEntity.setMax_student_count(maxStudentCount);
        courseEntity.setCourse_profile(courseProfile);
        courseEntity.setTextbook(textbook);
        courseEntity.setCredits_count(creditsCount);
        courseEntity.setAll_score(allScore);
        courseEntity.setPass_score(passScore);
        /*test data*/
//        courseEntity.setTeacher_user_id(teacherId);
//        courseEntity.setTeacher_realname("yc");
        ApiResponse apiResponse = JSON.parseObject(teacherApi.getTeacherInfo(teacherId), ApiResponse.class);
        if (!apiResponse.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
            System.out.println("edit fail");
        }
        TeacherDTO teacherDTO = JSON.parseObject(JSON.toJSONString(apiResponse.getData()), TeacherDTO.class);
        courseEntity.setTeacher_user_id(teacherId);
        courseEntity.setTeacher_realname(teacherDTO.getRealname());
//        System.out.println("edit success");
        courseMapper.editCourse(courseEntity);
    }

    @Override
    public int deleteCourse(int id) {
        int i = 0;
        try {
            i = courseMapper.deleteCourse(id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }

    @Override
    public CourseEntity viewCourse(int id) {
        return courseMapper.viewCourse(id);
    }

    @Override
    public Map viewSelectedStudents(int id) {
        Map<Integer, String> map = new HashMap<>();
        for (UserCourseEntity userCourseEntity : userCourseMapper.viewSelectedStudents(id)) {
            map.put(userCourseEntity.getStudent_user_id(), userCourseEntity.getStudent_realname());
        }
        return map;
    }

    @Override
    public List<UserSelectCourseDTO> selectedCourses(int studentId) {
        List<UserCourseEntity> userCourseEntities=userCourseMapper.selectedCoursesOfStudentId(studentId);
        List<UserSelectCourseDTO> myCourses=new ArrayList<>();
        userCourseEntities.forEach(userCourseEntity -> {
            UserSelectCourseDTO dto=new UserSelectCourseDTO();
            dto.setCourseId(userCourseEntity.getCourse_id());
            dto.setCourseCode(userCourseEntity.getCourse_code());
            dto.setCourseName(userCourseEntity.getCourse_name());
            dto.setTeacherRealname(userCourseEntity.getTeacher_realname());
            CourseEntity courseEntity=courseMapper.viewCourse(userCourseEntity.getCourse_id());
            dto.setSectionCount(courseEntity.getSection_count());
            dto.setCourseTime(courseEntity.getCourse_time());
            dto.setCourseProfile(courseEntity.getCourse_profile());
            dto.setTextbook(courseEntity.getTextbook());
            dto.setCreditsCount(courseEntity.getCredits_count());
            dto.setAllScore(courseEntity.getAll_score());
            dto.setPassScore(courseEntity.getPass_score());
            dto.setMyScore(userCourseEntity.getUser_score());
            myCourses.add(dto);
        });
        return myCourses;
    }


    @Override
    public String applyReExam(int courseId, int studentId) {
        CourseEntity courseEntities = viewCourse(courseId);
        int userScore = userCourseMapper.selectByCourseAndStudentId(courseId, studentId).getUser_score();
        if (userScore != 0) {
            if (userScore >= courseEntities.getPass_score()) {
                return "pass";
            } else {
                return "re";
            }
        } else {
            return "no test";
        }
    }

    @Override
    public Map myScore(int studentId) {
//        List<UserCourseEntity>userCourseEntities=userCourseMapper.myScore(studentId);
        Map<String, Integer> map = new HashMap<>();
        for (UserCourseEntity userCourseEntity : userCourseMapper.myScore(studentId)) {
            map.put(userCourseEntity.getCourse_name(), userCourseEntity.getUser_score());
        }
        return map;
//        return userCourseMapper.myScore(studentId);
    }

    @Override
    public int selectCourse(int courseId, int studentId) {
        UserCourseEntity userCourseEntity = new UserCourseEntity();
        if (userCourseMapper.selectByCourseAndStudentId(courseId, studentId) != null) {
            return 0;
        }
        CourseEntity courseEntity = courseMapper.viewCourse(courseId);
        if (courseEntity == null) {
            return 0;
        }
        userCourseEntity.setCourse_id(courseId);
        userCourseEntity.setCourse_name(courseEntity.getCourse_name());
        userCourseEntity.setCourse_code(courseEntity.getCourse_code());
        userCourseEntity.setTeacher_user_id(courseEntity.getTeacher_user_id());
        userCourseEntity.setTeacher_realname(courseEntity.getTeacher_realname());
        userCourseEntity.setStudent_user_id(studentId);
//        通过studentId获取学生信息
        ApiResponse apiResponse = JSON.parseObject(studentApi.studentInfo(studentId), ApiResponse.class);
//        System.out.println(apiResponse.getCode());
        if (!apiResponse.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
            return 0;
        }
        StudentDTO studentDTO = JSON.parseObject(JSON.toJSONString(apiResponse.getData()), StudentDTO.class);
        userCourseEntity.setStudent_realname(studentDTO.getRealname());
        userCourseEntity.setUser_score(0);
        return userCourseMapper.addUserCourse(userCourseEntity);
    }

    @Override
    public int cancelCourse(int courseId, int studentId) {
        return userCourseMapper.deleteStudentCourse(courseId,studentId);
    }

    @Override
    public UserSelectCourseDTO selectedCourseInfo(Integer studentId, Integer courseId) {
        CourseEntity courseEntity=courseMapper.viewCourse(courseId);
        UserCourseEntity userCourseEntity=userCourseMapper.selectByCourseAndStudentId(courseId,studentId);
            UserSelectCourseDTO dto=new UserSelectCourseDTO();
            dto.setCourseId(userCourseEntity.getCourse_id());
            dto.setCourseCode(userCourseEntity.getCourse_code());
            dto.setCourseName(userCourseEntity.getCourse_name());
            dto.setTeacherRealname(userCourseEntity.getTeacher_realname());
            dto.setSectionCount(courseEntity.getSection_count());
            dto.setCourseTime(courseEntity.getCourse_time());
            dto.setCourseProfile(courseEntity.getCourse_profile());
            dto.setTextbook(courseEntity.getTextbook());
            dto.setCreditsCount(courseEntity.getCredits_count());
            dto.setAllScore(courseEntity.getAll_score());
            dto.setPassScore(courseEntity.getPass_score());
            dto.setMyScore(userCourseEntity.getUser_score());
        return dto;
    }

}
